﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using trangbaocao.Models;
using System.Collections;
namespace trangbaocao.Controllers
{
    [AccessDeniedAuthorize]
    public class UserController : Controller
    {
        UserModel user_ = new UserModel();
        StationsModel StationsModel = new StationsModel();
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        //[trangbaocao.Controllers.HomeController.SessionExpireFilter]
        public ActionResult Index()
        {
            UserAssociateModel UserAssociateModel = new UserAssociateModel();
            List<UserAssociateModel> u = UserAssociateModel.getUser();                            
            return View(u);
            //return View();
        }
        PermissionModel PermissionModel = new PermissionModel();
        public ActionResult AddUser()
        {
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            user_.trungtam_id = new SelectList(main_stationModel, "id", "name");

            //List<StationsModel> stationsModel = StationsModel.getStations();
            //user_.tram_id = new SelectList(stationsModel, "id", "name");
            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(user_.trungtam_id.Skip(0).First().Value));
            user_.tram_id = new SelectList(stationModel, "id", "name");

            List<PermissionModel> Per = PermissionModel.getPermission();
            user_.permission_id = new SelectList(Per, "id", "name");
            return View(user_);
        }
        [HttpPost]
        public ActionResult AddUser(UserModel us)
        {
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            List<StationsModel> stationsModel = StationsModel.getStationsByMain_Stations(us.main_stations_id);
            List<PermissionModel> Per = PermissionModel.getPermission();
            if (ModelState.IsValid)
            {
                if (user_.checkExist(us) > 0)
                {
                    TempData["msg"] = "Tên đăng nhập đã tồn tại!";
                    us.tram_id = new SelectList(stationsModel, "id", "name");
                    us.trungtam_id = new SelectList(main_stationModel, "id", "name");
                    us.permission_id = new SelectList(Per, "id", "name");
                    return View(us);
                }
                us.status = Convert.ToInt16(Request["status"]);
                user_.addUser(us);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Index");
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            us.tram_id = new SelectList(stationsModel, "id", "name");
            us.trungtam_id = new SelectList(main_stationModel, "id", "name");
            us.permission_id = new SelectList(Per, "id", "name");
            return View(us);
        }
        
        public ActionResult EditUser(int id)
        {
            UserModel us = user_.getUser().Find(delegate(UserModel u) { return u.id == id; });
            //List<StationsModel> stationsModel = StationsModel.getStations();
            //us.tram_id = new SelectList(stationsModel, "id", "name");
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            us.trungtam_id = new SelectList(main_stationModel, "id", "name");

            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(us.main_stations_id);
            us.tram_id = new SelectList(stationModel, "id", "name");
            
            List<PermissionModel> Per = PermissionModel.getPermission();
            us.permission_id = new SelectList(Per, "id", "name");

            return View(us);
        }
        [HttpPost]
        public ActionResult EditUser(UserModel us)
        {
            List<StationsModel> stationsModel = StationsModel.getStations();
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            List<PermissionModel> Per = PermissionModel.getPermission();
            if (ModelState.IsValid)
            {
                us.status = Convert.ToInt16(Request["status"]);
                user_.updateUser(us);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Index");
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            us.tram_id = new SelectList(stationsModel, "id", "name");
            us.trungtam_id = new SelectList(main_stationModel, "id", "name");
            us.permission_id = new SelectList(Per, "id", "name");
            return View(us);
        }
        public ActionResult EditCurrentUser(int id)
        {
            UserModel us = user_.getUser().Find(delegate(UserModel u) { return u.id == id; });
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            us.trungtam_id = new SelectList(main_stationModel, "id", "name");

            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(us.main_stations_id);
            us.tram_id = new SelectList(stationModel, "id", "name");

            List<PermissionModel> Per = PermissionModel.getPermission();
            us.permission_id = new SelectList(Per, "id", "name");

            return View(us);
        }
        [HttpPost]
        public ActionResult EditCurrentUser(UserModel us)
        {
            List<StationsModel> stationsModel = StationsModel.getStations();
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            List<PermissionModel> Per = PermissionModel.getPermission();
            if (ModelState.IsValid)
            {
                us.status = Convert.ToInt16(Request["status"]);
                user_.updateCurrentUser(us);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("ChangePassword");
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            us.tram_id = new SelectList(stationsModel, "id", "name");
            us.trungtam_id = new SelectList(main_stationModel, "id", "name");
            us.permission_id = new SelectList(Per, "id", "name");
            return View(us);
        }
        public ActionResult ChangePassword()
        {
            UserAssociateModel UserAssociateModel = new UserAssociateModel();
            if (Session != null)
            {
                string user_name = Convert.ToString(Session["user_name"]);
                List<UserAssociateModel> u = UserAssociateModel.getUserByName(user_name);
                return View(u);
            }
            else
            {
                List<UserAssociateModel> u = UserAssociateModel.getUser();
                return View(u);
            }
            
        }
        [HttpPost]
        public JsonResult getStationsJson(string MainStationId, string Order)
        {
            Device_StationsModel Device_StationsModel = new Device_StationsModel();
            return Json(Device_StationsModel.getStations(MainStationId, Order));
        }
        public ActionResult DeleteUser(int id)
        {
            user_.deleteUser(id);
            List<UserModel> u = user_.getUser();
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult CheckedAction(int[] Items)
        {
            foreach (int item in Items)
            {
                user_.deleteUser(item);
            }
            List<UserModel> u = user_.getUser();
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("Index");
        }
        public ActionResult User_Report_Index()
        {
            return View();
        }

        
        
    }
}